<?php
/**
 * 应用系统用户权限角色管理
 * @author hushuai
 *
 */
class CompanyAction extends CommonAction{
	
	
	/**
	 * 商户列表
	 */
	public function index(){

		if(IS_AJAX){
			
		}else{
			$this->display();
		}
	}
	
	/**
	 * 用户列表
	 */
	public function admin(){
		
		$company = 1;
		
		if(IS_AJAX){
			
			$desc    = $this->_param("orderDirection","trim","desc");;
			$order   = $this->_param("orderName","trim","addtime");
			$pageNum = $this->_param("pageNum","intval",C('PAGE_NUMS'));
			$nowPage = $this->_param("nowPage","intval",1);
			$role    = $this->_get("role","intval",0);
			
			$map   = array("role"=>$role, "company"=>$company, "pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
			$json  = sendRest("BDC/Admin/page",$map);
			
			//echo createRestUrl("BDC/Admin/page",$map);exit;
			
			$data  = $json['data'];
			$count = $data['count'];
			
			$totalPage = ceil($count/$pageNum);
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
			
			//城市
			$view  = sendRest("BDC/Outlet/dictAddress");
			$ciarr = $view['data']['zh-cn']['array'];
			
			foreach($data['list'] AS $row){
				
				$status = $row['status']==1 ? "启用" : "禁用";
				$city   = $ciarr[$row['cityid']];
				
				$entry  = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'outlet'   => $row['outlet'],
						'username' => $row['username'],
						'jobno'    => $row['jobno'],
						'realname' => $row['realname'],
						'mobile'   => $row['mobile'],
						'status'   => $status,
						'city'     => $city[0],
					 ),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
			
		}else{
			
			$map  = array("company"=>$company);
			$json = sendRest("BDC/Role/index",$map);
			$this->assign("role",$json['data']);

			$role    = $this->_param("role","intval",0);
			$this->assign("roleid",$role);
			
			$this->display();
		}
	}
	
	/**
	 * 设置用户的状态
	 */
	public function adminActive(){
		
		$id     = $this->_param("id"    ,"intval",0);
		$status = $this->_param("status","intval",0);
		$map    = array("id"=>$id, "status"=>$status);
		
		$json   = sendRest("BDC/Admin/active",$map);
		$this->ajaxReturn($json);
	}
	
	/**
	 * 添加商户用户
	 */
	public function adminAdd(){
		
		$company = $this->_param("company","intval",1);
		
		if(IS_POST){
			
			$json = sendRest("BDC/Admin/add", $_POST, "POST");
			$this->ajaxReturn($json);
		}else{
			
			$model  = D("Company");
			$map    = array("company"=>$company);
			$json   = sendRest("BDC/Role/index",$map);
			$this->assign("role",$json["data"]);
			
			//$json   = sendRest("BDC/Outlet/index",$map);
			//$this->assign("outlet",$json["data"]);
			
			//城市
			$data = sendRest("BDC/Outlet/dictAddress");
			$this->assign("address",$data['data']['zh-cn']['array']);
			
			$company = $model->search($company);
			$this->assign("company",$company);
			
			//dump($company);exit;
			
			$this->display();
		}
	}
	
	/**
	 * 修改商户用户
	 */
	public function adminEdit(){
		
		$company = $this->_param("company","intval",1);
		$id      = $this->_param("id","intval",0);
		
		if(IS_POST){
			
			$json = sendRest("BDC/Admin/edit", $_POST, "POST");
			$this->ajaxReturn($json);
		}else{
			
			$model   = D("Company");
			$detail  = $model->searchAdmin($id);
			$this->assign("detail",$detail);
			
			$map     = array("company"=>$detail['company']);
			$json    = sendRest("BDC/Role/index",$map);
			$this->assign("role",$json["data"]);
			
			/*$json   = sendRest("BDC/Outlet/index",$map);
			$this->assign("outlet",$json["data"]);*/
			
			//城市
			$data = sendRest("BDC/Outlet/dictAddress");
			$this->assign("address",$data['data']['zh-cn']['array']);
				
			$company = $model->search($detail['company']);
			$this->assign("company",$company);
			
			$this->display();
		}
	}
	
	/**
	 * 删除商户用户
	 */
	public function adminDel(){
		
		$map['val'] = $this->_param("val");
		$json = sendRest("BDC/Company/del",$map);
		
		$this->ajaxReturn($json);
	}
	
	/**
	 * 角色列表
	 */
	public function role(){
		
		$company = 1;
		
		if(IS_AJAX){
			
			$map   = array("company"=>$company);
			$json  = sendRest("BDC/Role/index",$map);
			$data  = $json['data'];
				
			foreach($data AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'      => $row['id'],
						'name'    => $row['name'],
						'title'   => $row['title'],
						'addtime' => date("Y-m-d",$row['addtime'])
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,"JSON");
			
		}else{
				
			$this->display();
		}
	}
	
	/**
	 * 新增角色
	 */
	public function roleAdd(){
		
		if(IS_POST){
			
			$_POST['company'] = 1;
			$_POST['name']    = "易达客";
			$_POST['outlet']  = 1;
			$json = sendRest("BDC/Role/add", $_POST, "POST");
			
		}else{
			
			$role_purview1 = C('ROLE_PURVIEW1'); //角色权限配置
			$role_purview2 = C('ROLE_PURVIEW2'); //时间权限配置
			$this->assign("role_purview1",$role_purview1);
			$this->assign("role_purview2",$role_purview2);
			
			$map  = array("role"=>0);
			$json = sendRest("BDC/Role/nodeByRole", $map);
			$this->assign("node", $json["data"]);
			
			$this->display();
		}
	}
	
	/**
	 * 修改角色
	 */
	public function roleEdit(){
		
		$id = $this->_param("id","intval",0);
		
		if(IS_POST){
			$_POST['noCheckRole'] = 1; //不传入权限id
			$json = sendRest("BDC/Role/edit", $_POST,"post");
			if($json['status'] == 1){
				import('@.ORG.RBAC');
			}
			$this->ajaxReturn($json);
		}else{
			
			$model = D("Company");
			$map   = array("role"=>$id);
			$json  = sendRest("BDC/Role/nodeByRole", $map);
			$this->assign("node", $json["data"]);

			$role_purview1 = C('ROLE_PURVIEW1'); //角色权限配置
			$role_purview2 = C('ROLE_PURVIEW2'); //时间权限配置
			$this->assign("role_purview1",$role_purview1);
			$this->assign("role_purview2",$role_purview2);
			
			$detail = $model->searchRole($id);
			$this->assign("detail",$detail);
			
			$this->display();
		}
	}
	
	/**
	 * 角色菜单
	 */
	public function roleMenu(){
	
		$id = $this->_param("id","intval",0);
	
		if(IS_POST){
				
			$json = sendRest("BDC/Role/menuEdit", $_POST, "POST");
			$this->ajaxReturn($json);
		}else{
				
			$model = D("Company");
			$map   = array("role"=>$id);
			$json  = sendRest("BDC/Role/menuByRole", $map);
			$this->assign("node", $json["data"]);
			$detail = $model->searchRole($id);
			$this->assign("detail",$detail);
				
			$this->display();
		}
	}
	
	/**
	 * 删除角色
	 */
	public function roleDel(){
		
		$id     = $this->_param("id"    ,"intval",0);
		$map    = array("id"=>$id);
		
		$json   = sendRest("BDC/Role/del",$map);
		$this->ajaxReturn($json);
	}
	
	/**
	 * 商户门店管理
	 */
	public function outlet(){
		
	if(IS_AJAX){
    		$desc    = $this->_get("orderDirection","trim",   "desc");;
    		$order   = $this->_get("orderName",     "trim",   "addtime");
    		$pageNum = $this->_get("pageNum",       "intval",  C('PAGE_NUMS'));
    		$nowPage = $this->_get("p",             "intval",  1);
    		$where   = $_POST['where'];
    		$cateid  = $this->_param("cateid","intval",0);
    			
    		$map   = array("where"=>$where,"pageNum"=>$pageNum, "p"=>$nowPage, "orderDirection"=>$desc ,"orderName"=>$order);
    			
    		$json  = sendRest("BDC/Outlet/index",$map);
    		$data  = $json['data'];
    		$count = $data['count'];
    		
    		$totalPage    = ceil($count/$pageNum);
    		$jsonData     = array('page'=>$nowPage,'total'=>$count,'totalPage'=>$totalPage);
    		
    		foreach($data['list'] AS $row){

    			//地址列表
    			$dict_address = sendRest("BDC/Outlet/dictAddress");
    			$city     = $dict_address['data']['zh-cn']['array'][$row['city']][0];
    			$province = $dict_address['data']['zh-cn']['array'][$row['area2']][0];
    			
    			$entry = array('id'=>$row['id'],
    				'cell'=>array(
    					'id'       => $row['id'],    //id
    					'title'    => $row['title'], //名称
    					'tel'      => $row['tel'],   //电话
    					'email'    => $row['email'], //电邮
    					'level'    => $row['level'], //星级
    					'city'     => $province.$city, //星级
    					'type'     => $row['type']==1?"加盟店":"直营店",  //类型
    					'isdel'    => $row['isdel'], //id
    					'addtime'  => date('Y-m-d',$row['addtime']),
    				),
    			);
    			$jsonData['rows'][] = $entry;
    		}
    		$this->ajaxReturn($jsonData,"JSON");
    		
    	}else{
    		//地址列表
    		$dict_address = sendRest("BDC/Outlet/dictAddress");
    		$this->assign("json",$dict_address['data']['zh-cn']['array']);
    		
    		//所有菜系
    		$cuisine = sendRest("BDC/Outlet/dictCuisine");
    		$this->assign("cuisine",$cuisine['data']['array']);
    		
    		$this->display();
    	}
	}
	
	/**
	 * 添加商户门店
	 */
	public function outletAdd(){
	
		if(IS_POST){
    		$data   = sendRest("BDC/Outlet/add",$_POST);
    		$this->ajaxReturn($data);
    	}else{
    		//所有菜系
    		$cuisine = sendRest("BDC/Outlet/dict_cuisine");
    		$this->assign("cuisine",$cuisine['data']['array']);
    		
    		//地址列表
    		$dict_address = sendRest("BDC/Outlet/dictAddress");
    		$this->assign("json",$dict_address['data']['zh-cn']['array']);
    		$this->assign("jsonen",$dict_address['data']['en-us']['array']);
    		
    		$this->display();
    	}
	}
	
	/**
	 * 修改商户门店
	 */
	public function outletEdit(){
	
		if(IS_POST){
    		$data = sendRest("BDC/Outlet/edit",$_POST);
    		$this->ajaxReturn($data);
    	}else{
    		//餐厅详情
    		$map['id'] = $this->_param("id","intval",0);
    		$data = sendRest("BDC/Outlet/detail",$map);
    		
    		//所有菜系
    		$cuisine = sendRest("BDC/Outlet/dictCuisine");
    		$this->assign("cuisine",$cuisine['data']['array']);
    		
    		//选择的菜系数组
    		$cuisineArr = explode(',',$data['data']['cuisine']);
    		$this->assign("cuisineArr",$cuisineArr);
    		
    		//地址列表
    		$dict_address = sendRest("BDC/Outlet/dictAddress");
    		$this->assign("json",$dict_address['data']['zh-cn']['array']);
    		$this->assign("jsonen",$dict_address['data']['en-us']['array']);
    		
    		//查询城市数据字典的级别
    		if($data['data']['area']){
    			$dict = D("Dict");
    			$row  = $dict->find($data['data']['area']);
    			$this->assign("row",$row);
    		}
    		
    		$this->assign("detail",$data['data']);
    		$this->display();
    	}
	}
	
	/**
	 * 删除门店
	 */
	public function outletDel(){
	
		$map['val'] = $this->_param("val","trim","");
    	$data = sendRest("BDC/Outlet/del",$map);
    	
		$this->ajaxReturn($data);
	}
	
	/**
	 * 添加标注信息点
	 */
	public function outletLbs(){
	
		$map['id']      = $this->_param("id","intval",0);
		$map['address'] = $this->_param("address","trim","");
	
		$data = sendRest("BDC/Outlet/outletLbs",$map);
	
		$this->assign("row",$data['data']['row']);
		$this->assign("address",$data['data']['address']);
	
		$this->display();
	}
	
	/**
	 * 个人资料管理
	 */
	public function step(){
		
		$company = $this->_param("company","intval",1);
		$id      = $this->_param("id","intval",0);
		
		if(IS_POST){
			
			$json = sendRest("BDC/Admin/edit", $_POST, "POST");
			$this->ajaxReturn($json);
		}else{
			
			$model   = D("Company");
			$detail  = $model->searchAdmin($id);
			$this->assign("detail",$detail);
			
			$map     = array("company"=>$detail['company']);
			$json    = sendRest("BDC/Role/index",$map);
			$this->assign("role",$json["data"]);
			
			/*$json   = sendRest("BDC/Outlet/index",$map);
			$this->assign("outlet",$json["data"]);*/
			
			//城市
			$data = sendRest("BDC/Outlet/dictAddress");
			$this->assign("address",$data['data']['zh-cn']['array']);
				
			$company = $model->search($detail['company']);
			$this->assign("company",$company);
			
			$this->display();
		}
	}
	
}
